package com.bw.zg2.view

import android.app.Activity
import android.util.Log
import com.blankj.utilcode.util.ToastUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

/**
 *
 * @ClassName: Test03
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/14 12:08 PM
 * @Version: 1.0
 */

fun main() {
   // list1(list)
    ss(2,3)
    str(name)
//    testNull()
}

//声明一个空参数，无返回值的函数 t1()
fun t1() {
    //不可变类型
    val a: Int = 2
    // a = 3 不能再次赋值
    //可变类型
    var name: String = "zz"
    name = "ddd"
}

//声明一个带参数的函数
fun t2(str: String) {
    print(str)
}

//声明一个带参数的函数,带返回值的函数
fun t3(a: Int, b: Int): Int {
    return a + b
}


//测试空安全
fun testNull(){
    var name:String? = null

    print(name?.length)
//    print(name!!.length)
}
//声明一个list
val list = listOf<Int>(1, 2, 3, 4, 5, 6, 7, 8, 9)

//listStr 不能被添加数据
val listStr = listOf<String>("a","b","c")

//listStr2 可以调用add（）
val listStr2 = mutableListOf<String>()

//val map = mapOf<String,Int>()
//val map2 = mutableMapOf<String,Int>()


fun ss(a:Int,b:Int){
//    Log.d("dd", (a+b).toString())
    println(a+b)
}

var name = "name"
var name2: String = "name"

fun str(n: String) {
    name = "dfddd"
    print(name)
//    Log.d("dd", name)
}




fun list1(list: List<Int>) {

    listStr2.add("a")
    listStr2.add("b")
    listStr2.add("c")
    listStr2.add("d")



    // list.filter 过滤list
    val list2 = list.filter {
        //过滤list重的偶数
        it % 2 == 0
    }

    list.forEach {
        if (it % 2 == 0) {
            print(it)
            print(it * 2)
        }
    }

    list2.forEach {
        print(it)
        print(it * 2)
    }



    //创建方法，模拟使用协程发送网络请求
    fun getDataNet() {
        //全局的协程作用域
        GlobalScope.launch(Dispatchers.IO) {
            //延时2S
            delay(2000)
        }
    }


    val listStr = listOf<String>("1", "2", "1fdsfdsfdsf")

    fun numA() {
        for (i in listStr.indices) {

        }

        listStr.forEach { it.length > 6 }
        var num = 0
        listStr.forEach {
//        num = num+it.length
            num += it.length
        }
        //求平均数
        num / listStr.size
    }
//过滤出来字符长度大于6的字符列表


    //flow 背压处理
    fun testFlow() {
        val flow = flow<Int> {
            for (i in 1..Int.MAX_VALUE) {
                emit(i)
            }
        }.buffer()//增加缓冲区

        val flow2 = flow<Int> {
            for (i in 1..Int.MAX_VALUE) {
                emit(i)
            }
        }.conflate()//
    }
}